<?php
// +----------------------------------------------------------------------
// | HkCms 行为管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use common\controller\AdminBase;
use think\Db;

class Behavior extends AdminBase
{
    //行为模型
    protected $behavior = NULL;

    public function _initialize() {

        parent::_initialize();

        $this->behavior = new \common\model\Behavior;
    }

    //行为列表
    public function index() 
    {
        $where = array();

        //搜索行为标识
        $keyword = input('get.keyword');

        if (!empty($keyword)) {

            $where['name'] = array('like', '%' . $keyword . '%');

            $this->assign('keyword', $keyword);
        }

        $query['m'] = request()->module();
        $query['c'] = request()->controller();
        $query['a'] = request()->action();

        $action = $this->behavior->where($where)->order(array("id" => "desc"))->paginate('10',false,['query'=>$query]);

        $this->assign('keyword',$keyword);

        $this->assign('data', $action);

        return $this->fetch();
    }

    //添加行为
    public function add() 
    {
        if ( request()->isPost() ) {

            $post = input('post.');

            if ($this->behavior->addBehavior($post)) {

                $this->success('添加成功，需要更新缓存后生效！', url('Behavior/index'));

            } else {

                $this->error($this->behavior->getError());

            }
        } 

        return $this->fetch();
    }

    //编辑行为
    public function edit() 
    {
        if ( request()->isPost() ) {

            $post = input('post.', '', '');

            if ($this->behavior->editBehavior($post)) {

                $this->success('修改成功，需要更新缓存后生效！', url('Behavior/index'));

            } else {
                
                $this->error($this->behavior->getError());
            }
        }

        $id = input('id', 0, 'intval');

        if (empty($id)) {

            $this->error('请选择需要编辑的行为！');
        }

        //查询出行为信息
        $info = $this->behavior->getBehaviorById($id);

        if (empty($info)) {

            $error = $this->behavior->getError();

            $this->error($error ? $error : '该行为不存在！');
        }

        $this->assign('info', $info);

        return $this->fetch();
        
    }

    //删除行为
    public function delete() 
    {
        $id = input('id', 0, 'intval');

        if (empty($id)) {

            $this->error('请指定需要删除的行为！');
        }

        //删除
        if ($this->behavior->delBehaviorById($id)) {

            $this->success('行为删除成功，需要更新缓存后生效！', url('Behavior/index'));

        } else {
            
            $error = $this->behavior->getError();

            $this->error($error ? $error : '删除失败！');
        }
    }

    //行为日志
    public function logs() 
    {
        if( request()->isPost() ){

            $this->redirect('behavior/logs',$_POST);
        }

        $wehre = array();

        $type = input('type', '', 'trim')?input('type', '', 'trim'):'ruleid';

        $keyword = input('keyword', '', 'trim');

        if ($type) {

            if ($type == 'guid') {

                $wehre[$type] = array('LIKE', "%{$keyword}%");

            } else {

                $wehre[$type] = $keyword;
            }

            $this->assign('type', $type);

            $this->assign('keyword', $keyword);
        }

        $model = Db::name('BehaviorLog');

        $count = $model->where($wehre)->count();

        $page = $this->page($count, 20);

        $data = $model->where($wehre)->limit($page->firstRow . ',' . $page->listRows)->order(array('id' => 'DESC'))->select();

        $this->assign('Page', $page->show());

        return $this->fetch();
    }

    //状态转换
    public function status() 
    {
        $id = input('id', 0, 'intval');

        if (empty($id)) {

            $this->error('请指定需要状态转换的行为！');
        }

        //状态转换
        if ($this->behavior->statusBehaviorById($id)) {

            $this->success('行为状态转换成功，需要更新缓存后生效！', url('Behavior/index'));
            
        } else {

            $error = $this->behavior->getError();

            $this->error($error ? $error : '状态转换失败！');
        }
    }
}
